<?php

namespace app\admin\model\nursery;

use think\Model;


class Wantbuy extends Model
{

    

    

    // 表名
    protected $name = 'nursery_wantbuy';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'lvl_text',
        'effective_time_text',
        'status_text'
    ];
    

    
    public function getLvlList()
    {
        return ['1' => __('Lvl 1'), '2' => __('Lvl 2'), '3' => __('Lvl 3'), '4' => __('Lvl 4')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getLvlTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lvl']) ? $data['lvl'] : '');
        $list = $this->getLvlList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEffectiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['effective_time']) ? $data['effective_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    protected function setEffectiveTimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
//    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
